<template>
	<view>
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF"
			status-bar left-icon="left" left-text="返回"
			title="确认订单" @clickLeft="back" />
			{{currentAddress}}
			<view class="uni-list">
				<radio-group @change="addressChange">
					<label class="uni-list-cell uni-list-cell-pd" v-for="(address, index) in userAddressList" :key="address.id">
						<view>
							<radio :value="address.id" :checked="address.id===currentAddress.id"/>
						</view>
						<view>{{address.userAddress}}</view>
					</label>
				</radio-group>
			</view>
			<view>
			<uni-table ref="table" border stripe emptyText="暂无更多数据">
				<uni-tr>
					<uni-th align="center">商品图片</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in cartList" :key="index">
					<uni-td>
						<uni-card >
							<image slot='cover' style="width: 100%;" :src="item.imgUrl"></image> 
							<text class="uni-body">{{item.skuName}}</text>
							<br>
							<text class="uni-body">价格:{{item.cartPrice}}</text>
							<text class="uni-body">数量:{{item.skuNum}}</text>
							<uni-collapse-item :title=item.checkedCoupon.couponName>
								<view class="content">
									<text class="text">{{item.checkedCoupon.rangeDesc}}</text>
									<br>
									<text class="text">{{item.checkedCoupon.payAmount}}</text>
								</view>
							</uni-collapse-item>
						</uni-card>
					</uni-td>
				</uni-tr>
			</uni-table>
			</view>
			<uni-easyinput errorMessage v-model="comment" focus placeholder="请输入内容"></uni-easyinput>
			总金额：{{totalAmount}}
			
			<button type="primary"  @click="submitOrder()">提交订单</button>
	</view>
</template>
<script>
import cartApi from '../../api/cart/cart.js'
import couponApi from '../../api/coupon/coupon.js'
import orderApi from '../../api/order/order.js'
import userApi from '../../api/user/user.js'
export default {
	components: {},
	data() {
		return {
			userAddressList:[],
			currentAddress: {},
			cartList:[],
			totalAmount:0,
			comment:''
		}
	},
	methods:{
		back() {
			uni.navigateBack({
				delta: 1
			})
		},
		addressChange(evt){
			//console.log(evt.detail.value)
			/* for (let i = 0; i < this.userAddressList.length; i++) {
				if (this.userAddressList[i].id === evt.detail.value) {
					this.currentAddress = this.userAddressList[i];
					break;
				}
			} */
			this.currentAddress = this.userAddressList.find((address)=>{
				return address.id == evt.detail.value
			})
			
		},
		submitOrder(){
			let orderDTO = {
				"userAddress":this.currentAddress,
				"cartList":this.cartList,
				"comment":this.comment
			}
			orderApi.submitOrder(orderDTO).then(res=>{
				if(res.ok){
					uni.showToast({
					  title: res.message
					})
				}else{
					uni.showToast({
					  title: res.data
					})
				}
			})
		}
	},		
	onLoad(options) {
		
		this.cartList = JSON.parse(options.selectedCartList);//JSON.stringify  -->  JSON.parse
		orderApi.confirmOrder(this.cartList).then(res=>{
			if(res.ok){
				
				this.userAddressList = res.data.userAddressList
				this.totalAmount = res.data.totalAmount
			}
		}) 
		userApi.findUserAddressList1().then(res=>{
			this.userAddressList = res
			if(this.userAddressList.length>0){
				this.currentAddress = this.userAddressList[0];
			}
		})
		
	}
}
</script>
<style>

</style>